package top.aiprogram.user.controller;


import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import top.aiprogram.user.entity.Student;
import top.aiprogram.user.service.StudentService;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("user")
public class UserController {
//    @RequestMapping("/test")
//    public ModelAndView testUser(){
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("user");
//        User user = new User();
//        user.setId(20);
//        user.setName("laozhang");
//        modelAndView.addObject("user",user);
//        return modelAndView;
//    }
//    @RequestMapping("/demo")
//    public String demoUser(ModelMap modelMap){
//        User user = new User();
//        user.setId(20);
//        user.setName("laozhang");
//        modelMap.addAttribute("user",user);
//        return "user";
//    }
//    @RequestMapping("/test")///{id}/{name}
//    public String testUser(ModelMap modelMap,User user){//HttpServletRequest request){//@PathVariable("id") Integer sid,@PathVariable String name
//        String id = request.getParameter("id");
//        String name = request.getParameter("name");

//        System.out.println(user.getName());
//        System.out.println(user.getId());

//        User user = new User();
//        user.setName("laozhang");
//        user.setId(20);
//        modelMap.addAttribute("user",user);
//        return "redirect:demo";
//    }
//    @RequestMapping(value = "/test")//,method= RequestMethod.POST)
//    public User testUser(@RequestBody User user){
//        User user = new User();
//        user.setId(20);
//        user.setName("laozhang");
//        return user;
//    }
    @Autowired
    private StudentService studentService;
    @RequestMapping(value = "/test")
    private String testUser(@RequestBody Student student){
        studentService.createStudent(student);
        return "user";
    }
    @RequestMapping("/demo")
    public String demoUser(){

        return  "demo";
    }
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(MultipartFile multipartFile, HttpRequest request) throws IOException {
        if(request instanceof MultipartHttpServletRequest) {
            String name = multipartFile.getOriginalFilename();
            File file = new File("/" + name);
            multipartFile.transferTo(file);
        }
        return "demo";
    }
    @RequestMapping("download")
    public ResponseEntity<byte[]> downLoad() throws IOException {
    String path = "E:\\1.PNG";
        //path = URLEncoder.encode(path,"utf-8");
        File file = new File(path);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment",path);
        httpHeaders.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.CREATED);
    }
}
